package com.zhuhjay.app.web.controller.system;

import com.zhuhjay.common.base.controller.BaseController;
import com.zhuhjay.service.system.model.dto.MenuDto;
import com.zhuhjay.service.system.model.entity.SystemMenu;
import com.zhuhjay.service.system.model.vo.MenuInfoVo;
import com.zhuhjay.service.system.service.SystemMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Zhuhjay
 * @since 2023-02-25
 */
@Api(tags = "菜单管理")
@RestController
@RequestMapping("/menu")
public class SystemMenuController extends BaseController {

    @Resource
    private SystemMenuService menuService;

    @ApiOperation("获取菜单树形列表")
    @PreAuthorize("@perm.hasPerm('system:authority:menu:list')")
    @GetMapping("/tree/list")
    public List<MenuInfoVo> list() {
        return menuService.getMenusTreeList();
    }

    @ApiOperation("获取菜单列表")
    @PreAuthorize("@perm.hasPerm('system:authority:menu:list')")
    @GetMapping("/list")
    public List<SystemMenu> listAll() {
        return menuService.lambdaQuery().orderByAsc(SystemMenu::getSort).list();
    }

    @ApiOperation("添加菜单信息")
    @PreAuthorize("@perm.hasPerm('system:authority:menu:add')")
    @PostMapping("/add")
    public void add(@Valid @RequestBody MenuDto dto) {
        menuService.add(dto);
    }

    @ApiOperation("修改菜单信息")
    @PreAuthorize("@perm.hasPerm('system:authority:menu:update')")
    @PutMapping("/update/{id}")
    public void update(@PathVariable Integer id, @Valid @RequestBody MenuDto dto) {
        menuService.update(id, dto);
    }

    @ApiOperation("删除菜单信息")
    @PreAuthorize("@perm.hasPerm('system:authority:menu:delete')")
    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable Integer id) {
        menuService.delete(id);
    }

}
